package borge.web.teamspirit.client.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.interfaces.PersistentData;
import borge.web.teamspirit.client.interfaces.UserContentProvider;

/**
 * Database object.
 * Representation of a match.
 * Owned by a Serie entity.
 * @author Andreas Borglin
 *
 */
@SuppressWarnings("serial")
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Match implements PersistentData {

	public static enum Result{
		WIN,
		DRAW,
		LOSS
	}
	
	/* Encoded key generated by GAE when first saved.
	 * Guaranteed to be unique and contains mapping to 
	 * a team using gae.pk-name key defined below. */
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    private String iEncodedKey;
    
    
    @Persistent
	private String iOwningSerieKey;
    
    @Persistent
    private String iOpponents;
    
    @Persistent
	private Date iDateTime;
    
    @Persistent
	private Date iGatheringTime;
    
    // Shouldn't we move these to Serie/League?
    @Persistent
	private Integer iPeriods;
    
    @Persistent
	private Integer iPeriodLengthInMinutes;
    
    @Persistent
	private String iCity;
    
    @Persistent
	private String iStadium;
    
    @Persistent
	private String iField;
    
    @Persistent
	private Integer iOurScore;
    
    @Persistent
	private Integer iOpponentScore;
    
    @Persistent
	private Integer iCupRound;
    
    @Persistent
	private String iOtherInfo;

    // Should we really fetch these directly?
    @NotPersistent//defaultFetchGroup = "true")
    private List<PersistentData> iPlayerStats;
    
    @NotPersistent
    private List<PersistentData> iComments;
    
    @NotPersistent
    private List<PersistentData> iAttendance;
    
//    @PersistenceCapable
//    @EmbeddedOnly
//    static class MatchPlayerStats implements Serializable {
//    	
//    	@Persistent 
//    	private String iPlayerKey;
//    	
//    	@Persistent
//    	private Integer iGoals;
//    	
//    	@Persistent
//    	private Integer iAssists;
//    	
//    	@Persistent
//    	private Boolean iYellowCard;
//    	
//    	@Persistent
//    	private Boolean iRedCard;
//    	
//    	public MatchPlayerStats(){
//    		
//    	}
//    	
//		public void setPlayer(TeamPlayer aPlayer) {
//			this.iPlayerKey = aPlayer.getEncodedKey();
//		}
//
//		public String getPlayerKey() {
//			return iPlayerKey;
//		}
//
//		public void setGoals(Integer aGoals) {
//			this.iGoals = aGoals;
//		}
//
//		public Integer getGoals() {
//			return iGoals;
//		}
//
//		public void setAssists(Integer aAssists) {
//			this.iAssists = aAssists;
//		}
//
//		public Integer getAssists() {
//			return iAssists;
//		}
//
//		public void setYellowCard(Boolean aYellowCard) {
//			this.iYellowCard = aYellowCard;
//		}
//
//		public Boolean getYellowCard() {
//			return iYellowCard;
//		}
//
//		public void setRedCard(Boolean aRedCard) {
//			this.iRedCard = aRedCard;
//		}
//
//		public Boolean getRedCard() {
//			return iRedCard;
//		}
//    	
//    }
    
    public Match(){
    	
    }
	
	
    public static Match createMatchFromUserForm(Match aMatch, UserContentProvider aProvider) {

        if(aMatch == null)
            aMatch = new Match();
        
        aMatch.setOpponents(aProvider.getString(TeamSpirit.getConstants().opponents()));
        aMatch.setDateTime(aProvider.getDate(TeamSpirit.getConstants().date()));
        aMatch.setGatheringTime(aProvider.getDate(TeamSpirit.getConstants().gathering_time()));
        aMatch.setPeriods(aProvider.getInt(TeamSpirit.getConstants().periods()));
        aMatch.setPeriodLengthInMinutes(aProvider.getInt(TeamSpirit.getConstants().period_length()));
        aMatch.setCity(aProvider.getString(TeamSpirit.getConstants().city()));
        aMatch.setStadium(aProvider.getString(TeamSpirit.getConstants().stadium()));
        aMatch.setField(aProvider.getString(TeamSpirit.getConstants().field()));
        aMatch.setOurScore(aProvider.getInt(TeamSpirit.getConstants().our_score()));
        aMatch.setOpponentScore(aProvider.getInt(TeamSpirit.getConstants().opponent_score()));
        aMatch.setCupRound(aProvider.getInt(TeamSpirit.getConstants().cup_round())); 
        aMatch.setOtherInfo(aProvider.getString(TeamSpirit.getConstants().other_info()));
        
        return aMatch;
    }
    
    public void setOwningSerieKey(String aKey){
    	iOwningSerieKey = aKey;
    }
    
    public String getOwningSerieKey(){
    	return iOwningSerieKey;
    }
/*

    public void setiMatchId(Long aMatchId) {
        this.iMatchId = aMatchId;
    }


    public Long getMatchId() {
        return iMatchId;
    }
*/
    
	public String getEncodedKey(){
		return iEncodedKey;
	}
/*
    public void setSerieId(Long aSerieId) {
        this.iSerieId = aSerieId;
    }


    public Long getSerieId() {
        return iSerieId;
    }*/


    public void setDateTime(Date aDateTime) {
        this.iDateTime = aDateTime;
    }


    public Date getDateTime() {
        return iDateTime;
    }


    public void setGatheringTime(Date aGatheringTime) {
        this.iGatheringTime = aGatheringTime;
    }


    public Date getGatheringTime() {
        return iGatheringTime;
    }


    public void setPeriods(Integer aPeriods) {
        this.iPeriods = aPeriods;
    }


    public Integer getPeriods() {
        return iPeriods;
    }


    public void setPeriodLengthInMinutes(Integer aPeriodLengthInMinutes) {
        this.iPeriodLengthInMinutes = aPeriodLengthInMinutes;
    }


    public Integer getPeriodLengthInMinutes() {
        return iPeriodLengthInMinutes;
    }


    public void setCity(String aCity) {
        this.iCity = aCity;
    }


    public String getCity() {
        return iCity;
    }


    public void setStadium(String aStadium) {
        this.iStadium = aStadium;
    }


    public String getStadium() {
        return iStadium;
    }


    public void setField(String aField) {
        this.iField = aField;
    }


    public String getField() {
        return iField;
    }


    public void setOurScore(Integer aOurScore) {
        this.iOurScore = aOurScore;
    }


    public Integer getOurScore() {
        return iOurScore;
    }


    public void setOpponentScore(Integer aOpponentScore) {
        this.iOpponentScore = aOpponentScore;
    }


    public Integer getOpponentScore() {
        return iOpponentScore;
    }


    public void setCupRound(Integer aCupRound) {
        this.iCupRound = aCupRound;
    }


    public Integer getCupRound() {
        return iCupRound;
    }


    public void setOtherInfo(String aOtherInfo) {
        this.iOtherInfo = aOtherInfo;
    }


    public String getOtherInfo() {
        return iOtherInfo;
    }
    
    public void setOpponents(String aOpponents) {
        this.iOpponents = aOpponents;
    }


    public String getOpponents() {
        return iOpponents;
    }
    /*
	public void setComments(List<Comment> iComments) {
		this.iComments = iComments;
	}

	public List<Comment> getComments() {
		return iComments;
	}
	
	public void addComment(Comment aComment){
		iComments.add(aComment);
	}
	
	
	public void removeComment(Comment aComment){
		iComments.remove(aComment);
	}
	
	public void removeCommentAt(int aIndex){
		iComments.remove(aIndex);
	}
	
*/
	public void setPlayerStats(List<PersistentData> aPlayerStats) {
		this.iPlayerStats = aPlayerStats;
	}
	
	public void addPlayerStats(MatchPlayerStats aStats){
		iPlayerStats.add(aStats);
	}

	public List<PersistentData> getPlayerStats() {
		return iPlayerStats;
	}

	public void setComments(List<PersistentData> aComments) {
		iComments = aComments;
	}

	public List<PersistentData> getComments() {
		return iComments;
	}

	public void setAttendance(List<PersistentData> aAttendance) {
		iAttendance = aAttendance;
	}

	public List<PersistentData> getAttendance() {
		return iAttendance;
	}

	public Result getResult(){
		
		if(iOurScore > iOpponentScore)
			return Result.WIN;
		else if(iOurScore < iOpponentScore)
			return Result.LOSS;
		else
			return Result.DRAW;
		
	}
	
    public String dumpContent(){
        return new String("Dump" + 
                "\nId:" + iEncodedKey + 
                "\nOpponents:" + iOpponents +
                "\nDate:" + iDateTime +
                "\nGather time:" + iGatheringTime +
                "\nPeriods:" + iPeriods +
                "\nPeriodLength:" + iPeriodLengthInMinutes +
                "\nCity:" + iCity +
                "\nStadium:" + iStadium +
                "\nField:" + iField +
                "\nOur score:" + iOurScore +
                "\nTheir score:" + iOpponentScore +
                "\nCup round:" + iCupRound +
                "\nOther info:" + iOtherInfo);
    }

	@Override
	public String getDisplayingWidget() {
		// TODO Auto-generated method stub
		return null;
	}


}
